<?php

/**
 * [PHPFOX_HEADER]
 */
defined('PHPFOX') or exit('NO DICE!');

/**
 *
 *
 * @copyright		[PHPFOX_COPYRIGHT]
 * @author  		Raymond Benc
 * @package  		Module_Survey
 * @version 		$Id: survey.class.php 4545 2012-07-20 10:40:35Z Raymond_Benc $
 */
class Survey_Service_Browse extends Phpfox_Service {

    private $_aConds = array();
    private $_iCategory = false;
    private  $_iFollowerId = false;

    /**
     * Class constructor
     */
    public function __construct() {
        $this->_sTable = Phpfox::getT('survey');
    }

    public function setCondition($sCondition) {
        $this->_aConds[] = $sCondition;
    }
    
    public function follower($iId){
        $this->_iFollowerId = $iId;
    }
    
    public function get($iPage, $iLimit,$sOrder = 's.time_stamp DESC') {
        $this->_queryJoin();
        $iCnt = $this->database()->select("COUNT(*)")
                ->from(Phpfox::getT('survey'), 's')
                ->where($this->_aConds)
                ->execute('getSlaveField');
        $aSurveys = array();
        if ($iCnt > 0) {
            if (Phpfox::isUser() && Phpfox::isModule('like')) {
                $this->database()->select('lik.like_id AS is_liked, ')->leftJoin(Phpfox::getT('like'), 'lik', 'lik.type_id = \'survey\' AND lik.item_id = s.survey_id AND lik.user_id = ' . Phpfox::getUserId());
            }
            $sSelect = "s.*," . Phpfox::getUserField();
            if($this->_iFollowerId){
                $sSelect .= ",sf.user_id as is_followed";
            }
            $this->_queryJoin();
            $aSurveys = $this->database()->select($sSelect)
                    ->from(Phpfox::getT('survey'), 's')
                    ->join(Phpfox::getT("user"), "u", "s.user_id = u.user_id")
                    ->where($this->_aConds)
                    ->order($sOrder)
                    ->group("s.survey_id")
                    ->limit($iPage, $iLimit, $iCnt)
                    ->execute('getSlaveRows');
        }
        foreach ($aSurveys as $iKey => $aItem) {
            $aSurveys[$iKey] = $this->_processRow($aItem);
        }
        $this->_reset();
        return array($iCnt, $aSurveys);
    }

    public function _processRow($aItem) {
        $aItem['aFeed'] = array(
            'feed_view_comment' => true,
            'comment_type_id' => 'survey',
            'privacy' => $aItem['privacy'],
            'comment_privacy' => $aItem['privacy_comment'],
            'like_type_id' => 'survey',
            'feed_is_liked' => (isset($aItem['is_liked']) ? $aItem['is_liked'] : false),
            'feed_is_friend' => (isset($aItem['is_friend']) ? $aItem['is_friend'] : false),
            'item_id' => $aItem['survey_id'],
            'user_id' => $aItem['user_id'],
            'total_comment' => $aItem['total_comment'],
            'feed_total_like' => $aItem['total_like'],
            'total_like' => $aItem['total_like'],
            'feed_link' => Phpfox::getLib('url')->permalink('survey', $aItem['survey_id'], $aItem['title']),
            'feed_title' => $aItem['title'],
            'type_id' => 'survey'
        );
        $aItem['url'] = Phpfox::getLib('url')->permalink('survey', $aItem['survey_id'], $aItem['title']);
        return $aItem;
    }
    
    private function _queryJoin(){
        if($this->_iFollowerId){
            $this->database()->leftjoin(Phpfox::getT("survey_following"),"sf","s.survey_id = sf.survey_id AND sf.user_id = ".$this->_iFollowerId);
        }
    }
    
    private function _reset(){
        $this->_aConds = array();
        $this->_iFollowerId = false;
        $this->_iCategory = false;
    }

}

?>
